"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 功能
Case Name   : 升主过程中，原主网络故障
Create At   : 2023/05/23
Owner       : opentestcase043
Description :
        1.查询集群状态
        测试点：原主网络延迟故障
        2.主备切换并查看数据库的状态
        3.在原主上注入网络延迟故障
        4.将备机升级为主机
        5.清除原主故障
        6.查看数据库的状态
        测试点：原主网卡down
        7.主备切换并查看数据库的状态
        8.在原主上注入停止网卡故障
        9.将备机升级为主机
        10.恢复集群初始状态
        测试点：原主网络链路闪断
        11.主备切换并查看数据库的状态
        12.在原主上注入网络链路闪断
        13.将备机升级为主机
        14.查看数据库的状态
        15.恢复集群初始状态
        测试点：原主网络包乱序
        16.主备切换并查看数据库的状态
        17.在原主上注入网络包乱序故障
        18.将备机升级为主机
        19.清除原主故障
        20.等待故障结束，查看数据库状态
        21.清理环境
Expect      :
        1.集群状态正常
        2.切换成功，状态正常
        3.注入故障成功
        4.成功
        5.清除原主故障成功
        6.状态正常
        7.切换成功，状态正常
        8.注入故障成功
        9.失败
        10.恢复初始状态成功
        11.切换成功，状态正常
        12.注入故障成功
        13.成功
        14.状态异常
        15.恢复初始状态成功
        16.切换成功，状态正常
        17.注入故障成功
        18.成功
        19.成功
        20.状态正常
        21.清理环境成功
History     :
"""

import os
import unittest
import time

from testcase.utils.ComThread import ComThread
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Constant import Constant
from testcase.utils.Logger import Logger
from testcase.utils.Common import Common
from yat.test import Node

LOG = Logger()
Primary_SH = CommonSH('PrimaryDbUser')


@unittest.skipIf(1 == Primary_SH.get_node_num(), '单机环境不执行')
class ShareStorage(unittest.TestCase):

    def setUp(self):
        LOG.info('-----this is setup-----')
        LOG.info(f'-----{os.path.basename(__file__)} start-----')
        self.con = Constant()
        self.com = Common()
        self.pri_root_node = Node('PrimaryRoot')
        self.pri_node = Node('PrimaryDbUser')
        self.s1_sh = CommonSH('Standby1DbUser')

        LOG.info('----获取网卡----')
        network_card_cmd = 'ifconfig'
        LOG.info(network_card_cmd)
        network_card_result = self.pri_root_node.sh(
            network_card_cmd).result()
        network_card_list = network_card_result.split('\n\n')
        LOG.info(network_card_list)
        self.networkcard = ''
        for network_card_item in network_card_list:
            if self.pri_node.db_host in network_card_item:
                self.networkcard = network_card_item.split(':')[0].strip()
                LOG.info(self.networkcard)
                break

    def test_share_storage(self):
        text = '-----step1:查询集群状态; expect:集群状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)
        res = status.splitlines()[-1].split('|')[0].split('6001')[1]
        self.assertIn('P Primary Normal', res, '执行失败:' + text)

        text = '-----step2:主备切换并查看数据库的状态; expect:切换成功，状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('switchover', '-A')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_A_success_msg, status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status,
                      '执行失败:' + text)

        text = '-----step3:在原主上注入网络延迟故障; expect:注入故障成功-----'
        LOG.info(text)
        inject_command = f"rNet_delay_Ex (dev, time, duration) " \
                         f"values({self.networkcard}, 1000ms, 100)"
        cfe_thread = ComThread(
            self.com.execute_cfe,
            args=(self.pri_root_node, "inject", inject_command))
        cfe_thread.setDaemon(True)
        cfe_thread.start()
        cfe_thread.join()
        query_command = f" rNet_delay where dev={self.networkcard}"
        time.sleep(5)
        result = self.com.execute_cfe(self.pri_root_node, "query",
                                      query_command)
        LOG.info(result)
        self.assertIn("1.0s", result, '执行失败:' + text)

        text = '-----step4:将备机升级为主机; expect:成功-----'
        LOG.info(text)
        time.sleep(10)
        status = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_success_msg, status,
                      '执行失败:' + text)

        text = '-----step5:清除原主故障; expect:清除原主故障成功-----'
        LOG.info(text)
        clean_command = f" rNet_delay where dev={self.networkcard} " \
                        f"where time=1000ms"
        result = self.com.execute_cfe(self.pri_root_node, "clean",
                                      clean_command)
        self.assertNotIn("1.0s", result, '执行失败:' + text)

        text = '-----step6:查看数据库的状态; expect:状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_success_msg, status,
                      '执行失败:' + text)

        text = '-----step7:主备切换并查看数据库的状态; expect:切换成功，状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('switchover', '-A')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_A_success_msg, status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status,
                      '执行失败:' + text)

        text = '-----step8:在原主上注入停止网卡故障; expect:注入故障成功-----'
        LOG.info(text)
        inject_command = f"rNet_down (dev, time) " \
                         f"values({self.networkcard}, 30)"
        cfe_thread = ComThread(
            self.com.execute_cfe,
            args=(self.pri_root_node, "inject", inject_command))
        cfe_thread.setDaemon(True)
        cfe_thread.start()
        cfe_thread.join()

        text = '-----step9:将备机升级为主机; expect:失败-----'
        LOG.info(text)
        time.sleep(10)
        status = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status)
        self.assertNotIn(self.con.cm_switchover_success_msg, status,
                         '执行失败:' + text)

        text = '-----step10:恢复集群初始状态; expect:恢复初始状态成功-----'
        LOG.info(text)
        time.sleep(30)
        status = Primary_SH.exec_cm_ctl('stop')
        self.assertIn(self.con.cm_stop_success_msg, status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('start')
        self.assertIn(self.con.cm_start_success_msg, status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_success_msg, status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)
        res = status.splitlines()[-1].split('|')[0].split('6001')[1]
        self.assertIn('P Primary Normal', res, '执行失败:' + text)

        text = '-----step11:主备切换并查看数据库的状态; expect:切换成功，状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('switchover', '-A')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_A_success_msg, status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status,
                      '执行失败:' + text)

        text = '-----step12:在原主上注入网络链路闪断; expect:注入故障成功-----'
        LOG.info(text)
        inject_command = f"rNetLink_flash (repeat,duration,interval,dev) " \
                         f"values(10,2,5,{self.networkcard})"
        cfe_thread = ComThread(
            self.com.execute_cfe,
            args=(self.pri_root_node, "inject", inject_command))
        cfe_thread.setDaemon(True)
        cfe_thread.start()
        cfe_thread.join()

        text = '-----step13:将备机升级为主机; expect:失败-----'
        LOG.info(text)
        time.sleep(20)
        status = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status)
        self.assertNotIn(self.con.cm_switchover_success_msg, status,
                         '执行失败:' + text)

        text = '-----step14:查看数据库的状态; expect:状态异常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertNotIn('cluster_state   : Normal', status,
                         '执行失败:' + text)

        text = '-----step15:恢复集群初始状态; expect:恢复初始状态成功-----'
        LOG.info(text)
        time.sleep(50)
        status = Primary_SH.exec_cm_ctl('stop')
        self.assertIn(self.con.cm_stop_success_msg, status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('start')
        self.assertIn(self.con.cm_start_success_msg, status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_success_msg, status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)
        res = status.splitlines()[-1].split('|')[0].split('6001')[1]
        self.assertIn('P Primary Normal', res, '执行失败:' + text)

        text = '-----step16:主备切换并查看数据库的状态; expect:切换成功，状态正常-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('switchover', '-A')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_A_success_msg, status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status,
                      '执行失败:' + text)

        text = '-----step17:在原主上注入网络包乱序故障; expect:注入故障成功-----'
        LOG.info(text)
        inject_command = f"rNet_reorder_Ex (dev,time,rate,duration) " \
                         f"values({self.networkcard}, 3, 5%, 30)"
        cfe_thread = ComThread(
            self.com.execute_cfe,
            args=(self.pri_root_node, "inject", inject_command))
        cfe_thread.setDaemon(True)
        cfe_thread.start()
        time.sleep(10)
        query_command = f" rNet_reorder where dev={self.networkcard} " \
                        f"and time = 3 AND rate = 5%"
        result = self.com.execute_cfe(self.pri_root_node, "query",
                                      query_command)
        LOG.info(result)
        self.assertIn("5%", result, '执行失败:' + text)

        text = '-----step18:将备机升级为主机; expect:成功-----'
        LOG.info(text)
        status = Primary_SH.exec_cm_ctl('switchover', '-a')
        LOG.info(status)
        self.assertIn(self.con.cm_switchover_success_msg, status,
                      '执行失败:' + text)

        text = '-----step19:清除原主故障; expect:成功-----'
        LOG.info(text)
        clean_command = f" rNet_reorder where dev={self.networkcard} and" \
                        f" time = 3 AND rate = 5%"
        result = self.com.execute_cfe(self.pri_root_node, "clean",
                                      clean_command)
        self.assertNotIn("5%", result, '执行失败:' + text)

        text = '-----step20:查看数据库的状态; expect:状态正常-----'
        LOG.info(text)
        time.sleep(5)
        self.assertIn(self.con.cm_switchover_success_msg, status,
                      '执行失败:' + text)
        status = Primary_SH.exec_cm_ctl('query', '-Cv')
        LOG.info(status)
        self.assertIn('cluster_state   : Normal', status, '执行失败:' + text)
        res = status.splitlines()[-1].split('|')[0].split('6001')[1]
        self.assertIn('P Primary Normal', res, '执行失败:' + text)

    def tearDown(self):
        text = '-----this is tearDown-----'
        LOG.info(text)
        text = '-----step20:清理环境; expect:清理环境成功-----'
        LOG.info(text)
        time.sleep(50)
        self.status1 = ''
        self.status2 = ''
        query_command1 = f" rNet_delay where dev={self.networkcard}"
        result1 = self.com.execute_cfe(self.pri_root_node, "query",
                                       query_command1)
        LOG.info(result1)
        if "1.0s" in result1:
            clean_command1 = f"rNet_loss where dev={self.networkcard} " \
                            f"and rate=5%"
            self.status1 = self.com.execute_cfe(self.pri_root_node, "clean",
                                                clean_command1)
        query_command2 = f"rNet_reorder where dev={self.networkcard} "
        result2 = self.com.execute_cfe(self.pri_root_node, "query",
                                       query_command2)
        LOG.info(result2)
        if "%" in result2:
            clean_command2 = f" rNet_reorder where dev={self.networkcard} and "\
                             f"time = 3 AND rate = 5%"
            self.com.execute_cfe(self.pri_root_node, "clean", clean_command2)
            self.status2 = self.com.execute_cfe(self.pri_root_node, "query",
                                                query_command2)
        status3 = Primary_SH.exec_cm_ctl('stop')
        status4 = Primary_SH.exec_cm_ctl('start')
        status5 = Primary_SH.exec_cm_ctl('switchover', '-a')
        status6 = Primary_SH.exec_cm_ctl('query', '-Cv')
        if "1.0s" in self.status1:
            LOG.info(self.status1)
        if "%" in self.status2:
            LOG.info(self.status2)
        LOG.info(status3)
        LOG.info(status4)
        LOG.info(status5)
        LOG.info(status6)
        if "1.0s" in result1:
            self.assertNotIn("1.0s", self.status1, '执行失败:' + text)
        if "%" in result2:
            self.assertNotIn("%", self.status2, '执行失败:' + text)
        self.assertIn(self.con.cm_stop_success_msg, status3,
                      '执行失败:' + text)
        self.assertIn(self.con.cm_start_success_msg, status4,
                      '执行失败:' + text)
        self.assertIn(self.con.cm_switchover_success_msg, status5,
                      '执行失败:' + text)
        self.assertIn('cluster_state   : Normal', status6, '执行失败:' + text)
        res = status6.splitlines()[-1].split('|')[0].split('6001')[-1]
        self.assertIn('P Primary Normal', res, '执行失败:' + text)
        LOG.info(f'-----{os.path.basename(__file__)} end-----')
